﻿using System;
using System.Data;
using System.Configuration;
////using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
////using System.Xml.Linq;
using QuanCafe.DataLayer;


namespace QuanCafe.BusinessLayer
{
    public class CheckAdmin
    {
        public string activity;
        public string UserID;
        public string Username;
        public string Password;
        public string NewPass;
        public string OldPass;
        public string FullName;
        public string Email;
        public int Gender;
        public string DOB;
        public string DOJ;
        public string Note;
        public int Status;
        public int TopPermission;

        SQLHelper db = new SQLHelper();
        public CheckAdmin()
        {
            /////////
        }
        public DataTable GetAll()
        {
            try
            {
                db.AddParameter("@Activity", "GetAll", SqlDbType.NVarChar);
                return db.ExecuteReader("SP_Administrator", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataRow GetByID()
        {
            try
            {
                db.AddParameter("@Activity", "GetByID", SqlDbType.NVarChar);
                db.AddParameter("@UserName", Username, SqlDbType.NVarChar);                
                DataTable dtb = db.ExecuteReader("SP_Administrator", CommandType.StoredProcedure);

                if (dtb.Rows.Count > 0)
                    return dtb.Rows[0];
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string InsertUpdate()
        {
            try
            {
                db.AddParameter("@Activity", activity, SqlDbType.NVarChar);
                db.AddParameter("@UserName", Username, SqlDbType.NVarChar);
                db.AddParameter("@Password", Password, SqlDbType.NVarChar);
                db.AddParameter("@FullName", FullName, SqlDbType.NVarChar);
                db.AddParameter("@Email", Email, SqlDbType.NVarChar);
                db.AddParameter("@Gender", Gender, SqlDbType.Int);
                db.AddParameter("@Note", Note, SqlDbType.NVarChar);
                db.AddParameter("@DOB", DOB, SqlDbType.NVarChar);                
                db.AddParameter("@Status", Status, SqlDbType.Int);
                db.AddParameter("@TopPermission", TopPermission, SqlDbType.Int);
                return db.ExecuteScalar("SP_Administrator", CommandType.StoredProcedure).ToString();
            }
            catch
            {
                return null;
            }
        }
        public void ChangeActive()
        {
            try
            {
                db.AddParameter("@Activity", "ChangeActive", SqlDbType.NVarChar);
                db.AddParameter("@Username", this.Username.Trim(), SqlDbType.NVarChar);                
                db.ExecuteScalar("SP_Administrator", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool CheckAccount(ref bool isTopAdmin)
        {
            db.AddParameter("@Activity", "CheckAdmin", SqlDbType.NVarChar);
            db.AddParameter("@Username", this.Username.Trim(), SqlDbType.NVarChar);
            db.AddParameter("@Password", this.Password.Trim(), SqlDbType.NVarChar);
            DataTable dtb = db.ExecuteReader("SP_Administrator", CommandType.StoredProcedure);
            if (dtb.Rows.Count == 1)
            {
                isTopAdmin = dtb.Rows[0]["TopPermission"].ToString() == "1" ? true : false;
                return true;
            }
            else
                return false;
        }
        public DataRow Fullname()
        {
            try
            {
                db.AddParameter("@Activity", "GetFullname", SqlDbType.NVarChar);
                db.AddParameter("@Username", this.Username.Trim(), SqlDbType.NVarChar);
                DataTable dtb = db.ExecuteReader("SP_Administrator", CommandType.StoredProcedure);
                return dtb.Rows.Count > 0 ? dtb.Rows[0] : null;
            }
            catch
            {
                return null;
            }
        }
        #region Change password

        

        public void ChangePass()
        {   
            try
            {
                db.AddParameter("@Activity", "ChangePass", SqlDbType.NVarChar);
                db.AddParameter("@Username", this.Username.Trim(), SqlDbType.NVarChar);
                db.AddParameter("@OldPassword", this.OldPass.Trim(), SqlDbType.NVarChar);
                db.AddParameter("@NewPassword", this.NewPass.Trim(), SqlDbType.NVarChar);

                db.ExecuteScalar("SP_Administrator", CommandType.StoredProcedure);
            }
            catch 
            {
                //do nothing
            }
            
        }
        #endregion

        //kiem tra trung Account
        public bool CheckAccount()
        {
            try
            {
                db.AddParameter("@Activity", "CheckAccount", SqlDbType.NVarChar);
                db.AddParameter("@Username", Username, SqlDbType.NVarChar);
                db.AddParameter("@Password", Password, SqlDbType.NVarChar);
                string strResult = db.ExecuteScalar("SP_Administrator", CommandType.StoredProcedure);
                return strResult.ToLower() == "y" ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
